package com.info.threads;

import org.apache.log4j.Logger;
import org.net.operation.SendThread;
import org.net.operation.TerServer;

import com.info.server.Event;
import com.info.server.StartServer;
import com.info.utils.ConfigManager;
import com.info.utils.InfoPacket;
import com.info.utils.ProtocolUtil;
import com.info.utils.TimeTool;

/**
 * @author HAHA
 * 
 */
public class TerThread extends Thread {
	private static Logger logger = Logger.getLogger(TerThread.class);
	public static boolean flag = true;
	private boolean runningFlag;
	private Event argument = null;
	private SendThread sendThread = null;
	private ExecuteThread executeThread = null;
	private InfoPacket channelPack = null;

	private String terID = "";
	private String terID0x = "";
	private String serNum = "";
	private String serNum0x = "";
	private String infoLength = "";
	private String dateTime = ""; // 6
	private String latitude = ""; // 4
	private String longitude = ""; // 4
	private String statusBits = ""; // 1
	private String statusBits0x = ""; // 1
	private String station = ""; // 8
	private String stationlac = ""; // 8
	private String stationcellid = ""; // 8
	private String targetCode = "";// 1
	private String steps = ""; // 2
	private String distance = ""; // 2
	private String calorie = ""; // 4
	private String voltage = ""; // 1
	private String signal = ""; // 1
	private String reservation = ""; // 2
	private String velocity = ""; // 1
	private String course = ""; // 2
	private String targetReservation = ""; // 3
	private String isSuccess = ""; // 1
	private String ecgContent0x = "";
	private String ecgContent = "";
	private String heartRate = "";
	private String proNum = "";
	private String directions = "";
	private String contents = "";
	private String contents0x = "";
	private String systolic="";
	private String diastolic="";
	
	
	public boolean isRunning() {
		return this.runningFlag;
	}

	public synchronized void setRunning(boolean flag) {
		this.runningFlag = flag;
		if (flag)
			notify();
	}

	public Event getArgument() {
		return this.argument;
	}

	public void setArgument(Event string) {
		this.argument = string;
	}

	public TerThread(int threadNumber) {
		this.runningFlag = false;
		setName("ProcessInfoThread" + threadNumber);
		this.sendThread = new SendThread();
		this.sendThread.start();
		this.executeThread = new ExecuteThread();
		this.executeThread.start();

	}

	private void setNull() {
		this.terID = "";
		this.terID0x = "";
		this.serNum = "";
		this.serNum0x = "";
		this.infoLength = "";
		this.dateTime = ""; // 6
		this.latitude = ""; // 4
		this.longitude = ""; // 4
		this.statusBits = ""; // 1
		this.station = ""; // 8
		this.targetCode = "";// 1
		this.steps = ""; // 2
		this.distance = ""; // 2
		this.calorie = ""; // 4
		this.voltage = ""; // 1
		this.signal = ""; // 1
		this.reservation = ""; // 2
		this.velocity = ""; // 1
		this.course = ""; // 2
		this.targetReservation = ""; // 3
		this.isSuccess = ""; // 1
		this.ecgContent = "";
		this.ecgContent0x = "";
		this.heartRate = "";
		this.proNum = "";
		this.directions = "";
		this.contents = "";
		this.contents0x = "";
		this.systolic="";
		this.diastolic="";
	}

	public synchronized void run() {
		try {
			String str = "";
			StringBuffer result = null;
			while (flag) {
				result = new StringBuffer("");
				if (!this.runningFlag) {
					wait();
				} else {
					try {
						str = this.argument.getMsgInfo();
						// String InfoHead=str.substring(0, 2*2);
						logger.info("SHW#连接信息:" + str);
						if (this.argument != null
								&& str.startsWith(StartServer.MSG_START_FLAG)
								&& str.endsWith(StartServer.MSG_END_FLAG)) {
							str = str.substring(2 * 2);
							this.channelPack = this.argument.getUppack();
							Event msg = new Event();
							Event msgs = null;
							Event putMsg = null;
							
							this.setNull();
							if (this.channelPack != null) {
								proNum = str.substring(0, 1 * 2);
								int intNum = Integer.parseInt(proNum, 16);
								str = str.substring(1 * 2);
								switch (intNum) {
								case 129:// gps地址 81
									terID = str.substring(0, 6 * 2);
									str = str.substring(6 * 2);
									serNum0x = str.substring(0, 2 * 2);
									serNum = Integer.parseInt(serNum0x,
											16)+"";
									str = str.substring(2 * 2);
									infoLength = Integer.parseInt(
											str.substring(0, 1 * 2), 16)+"";
									str = str.substring(1 * 2);
									if (!this.channelPack.getType().equals("2")) {
										if (TerServer.terList.contains(terID)) {
											if (TerServer.channelHash
													.get(terID) != null) {
												InfoPacket uppacket = (InfoPacket) TerServer.channelHash
														.remove(terID);
												logger.info("SHW#终端:"
														+ uppacket.getId()
														+ "的ID重复,关闭上一个连接");
												uppacket.closeChannel();
											}
											this.channelPack.setType("2");
											this.channelPack.setId(terID);
											TerServer.channelHash.put(
													this.channelPack.getId(),
													this.channelPack);
										} else {
											logger.info("SHW#终端:" + terID
													+ "的ID未注册,关闭连接");
											setRunning(false);
											return;
										}
									}
									for (int i = 0; i < (Integer
											.parseInt(infoLength)) / 15; i++) {
										msgs = new Event();
										dateTime = str.substring(0, 6 * 2);
										dateTime = ProtocolUtil
												.getSimpleFormatDateTime(dateTime);
										str = str.substring(6 * 2);
										latitude = str.substring(0, 4 * 2);
										latitude = new Double(Long.parseLong(
												latitude, 16)) / 1800000 + "";
										/*if((latitude.indexOf(".")+7)<latitude.length())
											latitude=latitude.substring(0, latitude.indexOf(".")+7);*/
										str = str.substring(4 * 2);
										longitude = str.substring(0, 4 * 2);
										longitude = new Double(Long.parseLong(
												longitude, 16)) / 1800000 + "";
										/*if((longitude.indexOf(".")+7)<longitude.length())
											longitude=longitude.substring(0, longitude.indexOf(".")+7);*/
										str = str.substring(4 * 2);
										statusBits0x = str.substring(0, 1 * 2);
										statusBits = ProtocolUtil
												.str2ByteString(statusBits0x, 8);
										if (statusBits.substring(5, 6).equals(
												"0")) {
											longitude = "-" + longitude;
										}
										if (statusBits.substring(6, 7).equals(
												"0")) {
											latitude = "-" + latitude;
										}
										
										str = str.substring(1 * 2);
										msgs.setTerID(terID);
										msgs.setProNum(proNum);
										msgs.setSerNum(serNum);
										msgs.setInfoLength(infoLength);
										msgs.setDateTime(dateTime);
										msgs.setLatitude(latitude);
										msgs.setLongitude(longitude);
										msgs.setStatusBits(statusBits0x);
										msgs.setStation(station);
										msgs.setTargetCode(targetCode);
										msgs.setSteps(steps);
										msgs.setDistance(distance);
										msgs.setCalorie(calorie);
										msgs.setVoltage(voltage);
										msgs.setSignal(signal);
										msgs.setReservation(reservation);
										msgs.setVelocity(velocity);
										msgs.setCourse(course);
										msgs.setTargetReservation(targetReservation);
										msgs.setIsSuccess(isSuccess);
										msgs.setEcgContent(ecgContent);
										msgs.setHeartRate(heartRate);
										this.executeThread.addMsg(msgs);
									}

									result.append(StartServer.MSG_START_FLAG).append(proNum).append(terID)
											.append(serNum0x)
											.append(StartServer.ISuCCESS)
											.append(StartServer.MSG_END_FLAG);
									break;
								case 130:// 基站82

									terID = str.substring(0, 6 * 2);
									str = str.substring(6 * 2);
									serNum0x = str.substring(0, 2 * 2);
									serNum = Integer.parseInt(serNum0x,
											16)+"";
									str = str.substring(2 * 2);
									infoLength = Integer.parseInt(
											str.substring(0, 1 * 2), 16)+"";
									str = str.substring(1 * 2);
									if (!this.channelPack.getType().equals("2")) {
										if (TerServer.terList.contains(terID)) {
											if (TerServer.channelHash
													.get(terID) != null) {
												InfoPacket uppacket = (InfoPacket) TerServer.channelHash
														.remove(terID);
												logger.info("SHW#终端:"
														+ uppacket.getId()
														+ "的ID重复,关闭上一个连接");
												uppacket.closeChannel();
											}
											this.channelPack.setType("2");
											this.channelPack.setId(terID);
											TerServer.channelHash.put(
													this.channelPack.getId(),
													this.channelPack);
										} else {
											logger.info("SHW#终端:" + terID
													+ "的ID未注册,关闭连接");
											setRunning(false);
											return;
										}
									}
									for (int i = 0; i < (Integer
											.parseInt(infoLength)) / 15; i++) {
										msgs = new Event();
										dateTime = str.substring(0, 6 * 2);
										dateTime = ProtocolUtil
												.getSimpleFormatDateTime(dateTime);
										str = str.substring(6 * 2);
										station = str.substring(0, 8 * 2);
										str = str.substring(8 * 2);
										statusBits = str.substring(0, 1 * 2);
										str = str.substring(1 * 2);
										msgs.setTerID(terID);
										msgs.setProNum(proNum);
										msgs.setSerNum(serNum);
										msgs.setInfoLength(infoLength);
										msgs.setDateTime(dateTime);
										
										msgs.setStatusBits(statusBits);
										msgs.setStation(station);
										
										this.executeThread.addMsg(msgs);
									}

									result.append(StartServer.MSG_START_FLAG).append(proNum).append(terID)
											.append(serNum0x)
											.append(StartServer.ISuCCESS)
											.append(StartServer.MSG_END_FLAG);
									break;
								case 131:// 目标地址 下行83
									msgs = new Event();
									terID = str.substring(0, 6 * 2);
									str = str.substring(6 * 2);
									serNum = str.substring(0, 2 * 2);
									str = str.substring(2 * 2);
									isSuccess = str.substring(0, 1 * 2);
									proNum = "83";
									if (!this.channelPack.getType().equals("2")) {
										if (TerServer.terList.contains(terID)) {
											if (TerServer.channelHash
													.get(terID) != null) {
												InfoPacket uppacket = (InfoPacket) TerServer.channelHash
														.remove(terID);
												logger.info("SHW#终端:"
														+ uppacket.getId()
														+ "的ID重复,关闭上一个连接");
												uppacket.closeChannel();
											}
											this.channelPack.setType("2");
											this.channelPack.setId(terID);
											TerServer.channelHash.put(
													this.channelPack.getId(),
													this.channelPack);
										} else {
											logger.info("SHW#终端:" + terID
													+ "的ID未注册,关闭连接");
											setRunning(false);
											return;
										}
									}
									msgs.setTerID(terID);
									msgs.setProNum(proNum);
									msgs.setSerNum(serNum);
									msgs.setInfoLength(infoLength);
									msgs.setDateTime(dateTime);
									msgs.setContents(isSuccess);
									this.executeThread.addMsg(msgs);
									// result.append(proNum).append(terID).append("0003").append("02026B3F3E0C3D909B06");
									break;
								case 132:// 心率84
									terID = str.substring(0, 6 * 2);
									str = str.substring(6 * 2);
									serNum0x = str.substring(0, 2 * 2);
									serNum = Integer.parseInt(serNum0x,
											16)+"";
									str = str.substring(2 * 2);
									infoLength = Integer.parseInt(
											str.substring(0, 1 * 2), 16)+"";
									str = str.substring(1 * 2);

									dateTime = str.substring(0, 6 * 2);
									dateTime = ProtocolUtil
											.getSimpleFormatDateTime(dateTime);
									str = str.substring(6 * 2);
									if (!this.channelPack.getType().equals("2")) {
										if (TerServer.terList.contains(terID)) {
											if (TerServer.channelHash
													.get(terID) != null) {
												InfoPacket uppacket = (InfoPacket) TerServer.channelHash
														.remove(terID);
												logger.info("SHW#终端:"
														+ uppacket.getId()
														+ "的ID重复,关闭上一个连接");
												uppacket.closeChannel();
											}
											this.channelPack.setType("2");
											this.channelPack.setId(terID);
											TerServer.channelHash.put(
													this.channelPack.getId(),
													this.channelPack);
										} else {
											logger.info("SHW#终端:" + terID
													+ "的ID未注册,关闭连接");
											setRunning(false);
											return;
										}
									}
									for (int i = 0; i < Integer
											.parseInt(infoLength)-6; i++) {
										msgs = new Event();
										heartRate = str.substring(0, 1 * 2);
										str = str.substring(1 * 2);
										msgs.setTerID(terID);
										msgs.setProNum(proNum);
										msgs.setSerNum(serNum);
										msgs.setInfoLength(infoLength);
										msgs.setDateTime(dateTime);
										
										msgs.setHeartRate(heartRate);
										this.executeThread.addMsg(msgs);
									}
									result.append(StartServer.MSG_START_FLAG).append(proNum).append(terID)
											.append(serNum0x)
											.append(StartServer.ISuCCESS)
											.append(StartServer.MSG_END_FLAG);
									break;
								case 133:// 心电图85
									terID = str.substring(0, 6 * 2);
									str = str.substring(6 * 2);
									serNum0x = str.substring(0, 2 * 2);
									serNum = Integer.parseInt(serNum0x,
											16)+"";
									str = str.substring(2 * 2);
									infoLength = Integer.parseInt(
											str.substring(0, 1 * 2), 16)+"";
									str = str.substring(1 * 2);

									dateTime = str.substring(0, 6 * 2);
									dateTime = ProtocolUtil
											.getSimpleFormatDateTime(dateTime);
									str = str.substring(6 * 2);
									if (!this.channelPack.getType().equals("2")) {
										if (TerServer.terList.contains(terID)) {
											if (TerServer.channelHash
													.get(terID) != null) {
												InfoPacket uppacket = (InfoPacket) TerServer.channelHash
														.remove(terID);
												logger.info("SHW#终端:"
														+ uppacket.getId()
														+ "的ID重复,关闭上一个连接");
												uppacket.closeChannel();
											}
											this.channelPack.setType("2");
											this.channelPack.setId(terID);
											TerServer.channelHash.put(
													this.channelPack.getId(),
													this.channelPack);
										} else {
											logger.info("SHW#终端:" + terID
													+ "的ID未注册,关闭连接");
											setRunning(false);
											return;
										}
									}
									for (int i = 0; i < (Integer
											.parseInt(infoLength) - 6) / 2; i++) {
										msgs = new Event();
										msgs.setTerID(terID);
										msgs.setProNum(proNum);
										msgs.setSerNum(serNum);
										msgs.setInfoLength(infoLength);
										msgs.setDateTime(dateTime);
										ecgContent = str.substring(0, 2 * 2);
										str = str.substring(2 * 2);
										msgs.setEcgContent0x(ecgContent);
										String dd = new Double(
												Integer.parseInt(ecgContent, 16) * 0.005)
												/ 1023 * 1000 + "";
										try {
											dd = dd.substring(0,
													dd.indexOf(".") + 3);
										} catch (Exception e) {
										}
										msgs.setEcgContent(dd);
										this.executeThread.addMsg(msgs);
									}
									result.append(StartServer.MSG_START_FLAG).append(proNum).append(terID)
											.append(serNum0x)
											.append(StartServer.ISuCCESS)
											.append(StartServer.MSG_END_FLAG);
									break;
								case 134:// 计步器86
									msgs = new Event();
									terID = str.substring(0, 6 * 2);
									if (!this.channelPack.getType().equals("2")) {
										if (TerServer.terList.contains(terID)) {
											if (TerServer.channelHash
													.get(terID) != null) {
												InfoPacket uppacket = (InfoPacket) TerServer.channelHash
														.remove(terID);
												logger.info("SHW#终端:"
														+ uppacket.getId()
														+ "的ID重复,关闭上一个连接");
												uppacket.closeChannel();
											}
											this.channelPack.setType("2");
											this.channelPack.setId(terID);
											TerServer.channelHash.put(
													this.channelPack.getId(),
													this.channelPack);
										} else {
											logger.info("SHW#终端:" + terID
													+ "的ID未注册,关闭连接");
											setRunning(false);
											return;
										}
									}
									str = str.substring(6 * 2);
									serNum0x = str.substring(0, 2 * 2);
									serNum = Integer.parseInt(serNum0x,
											16)+"";
									str = str.substring(2 * 2);
									infoLength = Integer.parseInt(
											str.substring(0, 1 * 2), 16)+"";
									str = str.substring(1 * 2);
									
									for (int i = 0; i < (Integer
											.parseInt(infoLength)) / 14; i++) {
									dateTime = str.substring(0, 6 * 2);
									dateTime = ProtocolUtil
											.getSimpleFormatDateTime(dateTime);
									str = str.substring(6 * 2);
									steps = Integer.parseInt(str.substring(0, 2 * 2), 16)+"";
									str = str.substring(2 * 2);
									distance = Double
											.parseDouble(Integer.parseInt(str.substring(
															0, 2 * 2), 16)+"")
											/ 1000 + "";
									str = str.substring(2 * 2);
									calorie = Double
											.parseDouble(Integer.parseInt(str.substring(
															0, 4 * 2), 16)+"")
											/ 1000 + "";
									str = str.substring(4 * 2);
									msgs = new Event();
									msgs.setTerID(terID);
									msgs.setProNum(proNum);
									msgs.setSerNum(serNum);
									msgs.setInfoLength(infoLength);
									msgs.setDateTime(dateTime);
									msgs.setSteps(steps);
									msgs.setDistance(distance);
									msgs.setCalorie(calorie);
									this.executeThread.addMsg(msgs);}
									result.append(StartServer.MSG_START_FLAG).append(proNum).append(terID)
											.append(serNum0x)
											.append(StartServer.ISuCCESS)
											.append(StartServer.MSG_END_FLAG);
									break;
								case 135:// 血压87
									msgs = new Event();
									terID = str.substring(0, 6 * 2);
									str = str.substring(6 * 2);
									serNum0x = str.substring(0, 2 * 2);
									serNum = Integer.parseInt(serNum0x,
											16)+"";
									str = str.substring(2 * 2);
									infoLength = Integer.parseInt(
											str.substring(0, 1 * 2), 16)+"";
									str = str.substring(1 * 2);
									if (!this.channelPack.getType().equals("2")) {
										if (TerServer.terList.contains(terID)) {
											if (TerServer.channelHash
													.get(terID) != null) {
												InfoPacket uppacket = (InfoPacket) TerServer.channelHash
														.remove(terID);
												logger.info("SHW#终端:"
														+ uppacket.getId()
														+ "的ID重复,关闭上一个连接");
												uppacket.closeChannel();
											}
											this.channelPack.setType("2");
											this.channelPack.setId(terID);
											TerServer.channelHash.put(
													this.channelPack.getId(),
													this.channelPack);
										} else {
											logger.info("SHW#终端:" + terID
													+ "的ID未注册,关闭连接");
											setRunning(false);
											return;
										}
									}
									for (int i = 0; i < (Integer
											.parseInt(infoLength)) / 9; i++) {
									dateTime = str.substring(0, 6 * 2);
									dateTime = ProtocolUtil
											.getSimpleFormatDateTime(dateTime);
									str = str.substring(6 * 2);
									systolic = Integer.parseInt(str.substring(0, 1 * 2), 16)+"";
									str = str.substring(1 * 2);
									diastolic = Integer.parseInt(str.substring(0, 1 * 2), 16)+"";
									str = str.substring(1 * 2);
									heartRate = Integer.parseInt(str.substring(0, 1 * 2), 16)+"";
									str = str.substring(1 * 2);
									msgs = new Event();
									msgs.setTerID(terID);
									msgs.setProNum(proNum);
									msgs.setSerNum(serNum);
									msgs.setInfoLength(infoLength);
									msgs.setDateTime(dateTime);
									msgs.setSystolic(systolic);
									msgs.setDiastolic(diastolic);
									msgs.setHeartRate(heartRate);
									this.executeThread.addMsg(msgs);}
									result.append(StartServer.MSG_START_FLAG).append(proNum).append(terID)
											.append(serNum0x)
											.append(StartServer.ISuCCESS)
											.append(StartServer.MSG_END_FLAG);
									break;
								case 65:// gps数据 41
									msgs = new Event();
									reservation = str.substring(0, 2 * 2);
									str = str.substring(2 * 2);
									terID = str.substring(0, 6 * 2);
									str = str.substring(6 * 2);
									serNum0x = str.substring(0, 2 * 2);
									serNum = Integer.parseInt(serNum0x,
											16)+"";
									str = str.substring(2 * 2);
									infoLength = Integer.parseInt(
											str.substring(0, 1 * 2), 16)+"";
									str = str.substring(1 * 2);
									dateTime = str.substring(0, 6 * 2);
									dateTime = ProtocolUtil
											.getSimpleFormatDateTime(dateTime);
									str = str.substring(6 * 2);
									latitude = str.substring(0, 4 * 2);
									latitude = new Double(Long.parseLong(
											latitude, 16)) / 1800000 + "";
									/*if((latitude.indexOf(".")+6)<latitude.length())
										latitude=latitude.substring(0, latitude.indexOf(".")+7);*/
									str = str.substring(4 * 2);
									longitude = str.substring(0, 4 * 2);
									longitude = new Double(Long.parseLong(
											longitude, 16)) / 1800000 + "";
									/*if((longitude.indexOf(".")+6)<longitude.length())
										longitude=longitude.substring(0, longitude.indexOf(".")+7);*/
									str = str.substring(4 * 2);
									velocity = Integer.parseInt(
											str.substring(0, 1 * 2), 16)+"";
									str = str.substring(1 * 2);
									course = Integer.parseInt(
											str.substring(0, 2 * 2), 16)+"";
									str = str.substring(2 * 2);
									targetReservation = str.substring(0, 3 * 2);
									str = str.substring(3 * 2);
									statusBits0x = str.substring(0, 4 * 2);
									statusBits = ProtocolUtil.str2ByteString(
											statusBits0x, 32);
									if (statusBits.substring(29, 30)
											.equals("0")) {
										longitude = "-" + longitude;
									}
									if (statusBits.substring(30, 31)
											.equals("0")) {
										latitude = "-" + latitude;
									}
									str = str.substring(4 * 2);
									if (!this.channelPack.getType().equals("2")) {
										if (TerServer.terList.contains(terID)) {
											if (TerServer.channelHash
													.get(terID) != null) {
												InfoPacket uppacket = (InfoPacket) TerServer.channelHash
														.remove(terID);
												logger.info("SHW#终端:"
														+ uppacket.getId()
														+ "的ID重复,关闭上一个连接");
												uppacket.closeChannel();
											}
											this.channelPack.setType("2");
											this.channelPack.setId(terID);
											TerServer.channelHash.put(
													this.channelPack.getId(),
													this.channelPack);
										} else {
											logger.info("SHW#终端:" + terID
													+ "的ID未注册,关闭连接");
											setRunning(false);
											return;
										}
									}
									msgs.setTerID(terID);
									msgs.setProNum(proNum);
									msgs.setSerNum(serNum);
									msgs.setInfoLength(infoLength);
									msgs.setDateTime(dateTime);
									msgs.setLatitude(latitude);
									msgs.setLongitude(longitude);
									msgs.setStatusBits(statusBits0x);
									msgs.setStation(station);
									msgs.setTargetCode(targetCode);
									msgs.setSteps(steps);
									msgs.setDistance(distance);
									msgs.setCalorie(calorie);
									msgs.setVoltage(voltage);
									msgs.setSignal(signal);
									msgs.setReservation(reservation);
									msgs.setVelocity(velocity);
									msgs.setCourse(course);
									msgs.setTargetReservation(targetReservation);
									msgs.setIsSuccess(isSuccess);
									msgs.setEcgContent(ecgContent);
									msgs.setHeartRate(heartRate);
									this.executeThread.addMsg(msgs);
									result.append(StartServer.MSG_START_FLAG).append(proNum).append(reservation)
											.append(terID).append(serNum0x)
											.append(StartServer.MSG_END_FLAG);
									break;
								case 66:// 心跳包 42
									msgs = new Event();
									terID = str.substring(0, 6 * 2);
									str = str.substring(6 * 2);
									serNum0x = str.substring(0, 2 * 2);
									serNum = Integer.parseInt(serNum0x,
											16)+"";
									infoLength = "0";
									dateTime = TimeTool.getDateTimeSecond();
									str = str.substring(2 * 2);
									voltage = Integer.parseInt(
											str.substring(0, 1 * 2), 16)+"";
									str = str.substring(1 * 2);
									signal = Integer.parseInt(
											str.substring(0, 1 * 2), 16)+"";
									str = str.substring(1 * 2);
									if (!this.channelPack.getType().equals("2")) {
										if (TerServer.terList.contains(terID)) {
											if (TerServer.channelHash
													.get(terID) != null) {
												InfoPacket uppacket = (InfoPacket) TerServer.channelHash
														.remove(terID);
												logger.info("SHW#终端:"
														+ uppacket.getId()
														+ "的ID重复,关闭上一个连接");
												uppacket.closeChannel();
											}
											this.channelPack.setType("2");
											this.channelPack.setId(terID);
											TerServer.channelHash.put(
													this.channelPack.getId(),
													this.channelPack);
										} else {
											logger.info("SHW#终端:" + terID
													+ "的ID未注册,关闭连接");
											setRunning(false);
											return;
										}
									}
									msgs.setTerID(terID);
									msgs.setProNum(proNum);
									msgs.setSerNum(serNum);
									msgs.setInfoLength(infoLength);
									msgs.setDateTime(dateTime);
									
									msgs.setVoltage(voltage);
									msgs.setSignal(signal);
									
									this.executeThread.addMsg(msgs);
									result.append(StartServer.MSG_START_FLAG).append(proNum).append(
											StartServer.MSG_END_FLAG);
									break;
								case 67:// gps数据区间 下行 43
									msgs = new Event();
									reservation = ProtocolUtil.str2String(
											str.substring(0, 2 * 2), 2);
									str = str.substring(2 * 2);
									terID = str.substring(0, 6 * 2);
									str = str.substring(6 * 2);
									isSuccess = str.substring(0, 1 * 2);
									str = str.substring(1 * 2);
									if (!this.channelPack.getType().equals("2")) {
										if (TerServer.terList.contains(terID)) {
											if (TerServer.channelHash
													.get(terID) != null) {
												InfoPacket uppacket = (InfoPacket) TerServer.channelHash
														.remove(terID);
												logger.info("SHW#终端:"
														+ uppacket.getId()
														+ "的ID重复,关闭上一个连接");
												uppacket.closeChannel();
											}
											this.channelPack.setType("2");
											this.channelPack.setId(terID);
											TerServer.channelHash.put(
													this.channelPack.getId(),
													this.channelPack);
										} else {
											logger.info("SHW#终端:" + terID
													+ "的ID未注册,关闭连接");
											setRunning(false);
											return;
										}
									}
									msgs.setTerID(terID);
									msgs.setProNum(proNum);
									msgs.setSerNum(serNum);
									msgs.setInfoLength(infoLength);
									msgs.setDateTime(dateTime);
									msgs.setReservation(reservation);
									msgs.setContents(isSuccess);
									this.executeThread.addMsg(msgs);
									// result.append(proNum).append(reservation).append(terID).append("0A").append(StartServer.MSG_END_FLAG);
									break;
								case 0:// 连接指令 00
									msgs = new Event();
									terID = str.substring(0, 6 * 2);
									str = str.substring(6 * 2);
									directions = str.substring(0, 1 * 2);
									str = str.substring(1 * 2);
									infoLength = ProtocolUtil.str2String(
											str.substring(0, 1 * 2), 1);
									str = str.substring(1 * 2);
									contents0x = str.substring(0, 1 * 2);
									msgs.setTerID(terID);
									msgs.setProNum(proNum);
									msgs.setDirection(directions);
									msgs.setInfoLength(infoLength);
									msgs.setContents0x(contents0x);
									msgs.setContents(contents0x);
									if (!this.channelPack.getType().equals("2")) {
										if (TerServer.terList.contains(terID)) {
											if (TerServer.channelHash
													.get(terID) != null) {
												InfoPacket uppacket = (InfoPacket) TerServer.channelHash
														.remove(terID);
												logger.info("SHW#终端:"
														+ uppacket.getId()
														+ "的ID重复,关闭上一个连接");
												uppacket.closeChannel();
											}
											this.channelPack.setType("2");
											this.channelPack.setId(terID);
											TerServer.channelHash.put(
													this.channelPack.getId(),
													this.channelPack);
										} else {
											logger.info("SHW#终端:" + terID
													+ "的ID未注册,关闭连接");
											setRunning(false);
											return;
										}
									}
									break;
								case 1:// 设置时区 01
									msgs = new Event();
									terID = str.substring(0, 6 * 2);
									str = str.substring(6 * 2);
									directions = str.substring(0, 1 * 2);
									str = str.substring(1 * 2);
									infoLength = ProtocolUtil.str2String(
											str.substring(0, 1 * 2), 1);
									str = str.substring(1 * 2);
									contents0x = str.substring(0, Integer.parseInt(infoLength) * 2);
									msgs.setTerID(terID);
									msgs.setDirection(directions);
									msgs.setInfoLength(infoLength);
									msgs.setContents0x(contents0x);
									msgs.setProNum(proNum);
									if (directions
											.equals(ConfigManager.putBack)) {
										msgs.setContents(contents0x);
									} else if (directions
											.equals(ConfigManager.readBack)) {
										msgs.setContents(ConfigManager.timezone[Integer.parseInt(contents0x)]);
									}
									this.executeThread.addMsg(msgs);
									break;
								case 2:// 设置GPS时间校准 02
									msgs = new Event();
									terID = str.substring(0, 6 * 2);
									str = str.substring(6 * 2);
									directions = str.substring(0, 1 * 2);
									str = str.substring(1 * 2);
									infoLength = ProtocolUtil.str2String(
											str.substring(0, 1 * 2), 1);
									str = str.substring(1 * 2);
									contents0x = str.substring(0, Integer.parseInt(infoLength) * 2);
									msgs.setTerID(terID);
									msgs.setDirection(directions);
									msgs.setInfoLength(infoLength);
									msgs.setContents0x(contents0x);
									msgs.setProNum(proNum);
									msgs.setContents(contents0x);
									this.executeThread.addMsg(msgs);
									break;
								case 3:// 设置闹铃 03
									msgs = new Event();
									terID = str.substring(0, 6 * 2);
									str = str.substring(6 * 2);
									directions = str.substring(0, 1 * 2);
									str = str.substring(1 * 2);
									infoLength = ProtocolUtil.str2String(
											str.substring(0, 1 * 2), 1);
									str = str.substring(1 * 2);
									contents0x = str.substring(0, Integer.parseInt(infoLength) * 2);
									msgs.setTerID(terID);
									msgs.setDirection(directions);
									msgs.setInfoLength(infoLength);
									msgs.setContents0x(contents0x);
									msgs.setProNum(proNum);
									if (directions
											.equals(ConfigManager.putBack)) {
										msgs.setContents(contents0x);
									} else if (directions
											.equals(ConfigManager.readBack)) {
										StringBuffer sb=new StringBuffer("");
										for(int i=0;i<Integer.parseInt(infoLength)/3;i++){
											String info=contents0x.substring(0, 3*2);
											if(info.substring(0, 2).equals(ConfigManager.success)){
												sb.append("有效");
											}else{
												sb.append("无效");
											}
											sb.append(Integer.parseInt(info.substring(2, 4),16)+":");
											sb.append(Integer.parseInt(info.substring(4, 6),16));
											contents0x = contents0x.substring(3 * 2);
										}
										msgs.setContents(sb.toString());
									}
									this.executeThread.addMsg(msgs);
									break;
								case 4:// 设置速度 04
									msgs = new Event();
									terID = str.substring(0, 6 * 2);
									str = str.substring(6 * 2);
									directions = str.substring(0, 1 * 2);
									str = str.substring(1 * 2);
									infoLength = ProtocolUtil.str2String(
											str.substring(0, 1 * 2), 1);
									str = str.substring(1 * 2);
									contents0x = str.substring(0, Integer.parseInt(infoLength) * 2);
									msgs.setTerID(terID);
									msgs.setDirection(directions);
									msgs.setInfoLength(infoLength);
									msgs.setContents0x(contents0x);
									msgs.setProNum(proNum);
									if (directions
											.equals(ConfigManager.putBack)) {
										msgs.setContents(contents0x);
										GetPutInfoThread.hm.remove(terID);
									} else if (directions
											.equals(ConfigManager.readBack)) {
										StringBuffer sb=new StringBuffer("");
											if(contents0x.substring(0, 2).equals(ConfigManager.success)){
												sb.append("有效");
											}else{
												sb.append("无效");
											}
											sb.append(ProtocolUtil.str2String(contents0x.substring(2, 6),4));
										msgs.setContents(sb.toString());
									}
									this.executeThread.addMsg(msgs);
									break;
								case 5:// 设置高度 05
									msgs = new Event();
									terID = str.substring(0, 6 * 2);
									str = str.substring(6 * 2);
									directions = str.substring(0, 1 * 2);
									str = str.substring(1 * 2);
									infoLength = ProtocolUtil.str2String(
											str.substring(0, 1 * 2), 1);
									str = str.substring(1 * 2);
									contents0x = str.substring(0, Integer.parseInt(infoLength) * 2);
									msgs.setTerID(terID);
									msgs.setDirection(directions);
									msgs.setInfoLength(infoLength);
									msgs.setContents0x(contents0x);
									msgs.setProNum(proNum);
									if (directions
											.equals(ConfigManager.putBack)) {
										msgs.setContents(contents0x);
									} else if (directions
											.equals(ConfigManager.readBack)) {
										StringBuffer sb=new StringBuffer("");
											if(contents0x.substring(0, 2).equals(ConfigManager.success)){
												sb.append("有效");
											}else{
												sb.append("无效");
											}
											sb.append(ProtocolUtil.str2String(contents0x.substring(2, 6),4));
										msgs.setContents(sb.toString());
									}
									this.executeThread.addMsg(msgs);
									break;
								case 6:// 设置温度 06
									msgs = new Event();
									terID = str.substring(0, 6 * 2);
									str = str.substring(6 * 2);
									directions = str.substring(0, 1 * 2);
									str = str.substring(1 * 2);
									infoLength = ProtocolUtil.str2String(
											str.substring(0, 1 * 2), 1);
									str = str.substring(1 * 2);
									contents0x = str.substring(0, Integer.parseInt(infoLength) * 2);
									msgs.setTerID(terID);
									msgs.setDirection(directions);
									msgs.setInfoLength(infoLength);
									msgs.setContents0x(contents0x);
									msgs.setProNum(proNum);
									if (directions
											.equals(ConfigManager.putBack)) {
										msgs.setContents(contents0x);
									} else if (directions
											.equals(ConfigManager.readBack)) {
										StringBuffer sb=new StringBuffer("");
											if(contents0x.substring(0, 2).equals(ConfigManager.success)){
												sb.append("有效");
											}else{
												sb.append("无效");
											}
											sb.append(ProtocolUtil.str2String(contents0x.substring(2, 6),4));
										msgs.setContents(sb.toString());
									}
									this.executeThread.addMsg(msgs);
									break;
								case 7:// 设置气压 07
									msgs = new Event();
									terID = str.substring(0, 6 * 2);
									str = str.substring(6 * 2);
									directions = str.substring(0, 1 * 2);
									str = str.substring(1 * 2);
									infoLength = ProtocolUtil.str2String(
											str.substring(0, 1 * 2), 1);
									str = str.substring(1 * 2);
									contents0x = str.substring(0, Integer.parseInt(infoLength) * 2);
									msgs.setTerID(terID);
									msgs.setDirection(directions);
									msgs.setInfoLength(infoLength);
									msgs.setContents0x(contents0x);
									msgs.setProNum(proNum);
									if (directions
											.equals(ConfigManager.putBack)) {
										msgs.setContents(contents0x);
									} else if (directions
											.equals(ConfigManager.readBack)) {
										StringBuffer sb=new StringBuffer("");
											if(contents0x.substring(0, 2).equals(ConfigManager.success)){
												sb.append("有效");
											}else{
												sb.append("无效");
											}
											sb.append(ProtocolUtil.str2String(contents0x.substring(2, 6),4));
										msgs.setContents(sb.toString());
									}
									this.executeThread.addMsg(msgs);
									break;
								case 8:// 设置LCD亮度和对比度 08
									msgs = new Event();
									terID = str.substring(0, 6 * 2);
									str = str.substring(6 * 2);
									directions = str.substring(0, 1 * 2);
									str = str.substring(1 * 2);
									infoLength = ProtocolUtil.str2String(
											str.substring(0, 1 * 2), 1);
									str = str.substring(1 * 2);
									contents0x = str.substring(0, Integer.parseInt(infoLength) * 2);
									msgs.setTerID(terID);
									msgs.setDirection(directions);
									msgs.setInfoLength(infoLength);
									msgs.setContents0x(contents0x);
									msgs.setProNum(proNum);
									if (directions
											.equals(ConfigManager.putBack)) {
										msgs.setContents(contents0x);
									} else if (directions
											.equals(ConfigManager.readBack)) {
										StringBuffer sb=new StringBuffer("");
											sb.append(Integer.parseInt(contents0x.substring(0, 2),16)).append(Integer.parseInt(contents0x.substring(2, 4),16));
										msgs.setContents(sb.toString());
									}
									this.executeThread.addMsg(msgs);
									break;
								case 9:// 设置SOS号码 09
									msgs = new Event();
									terID = str.substring(0, 6 * 2);
									str = str.substring(6 * 2);
									directions = str.substring(0, 1 * 2);
									str = str.substring(1 * 2);
									infoLength = ProtocolUtil.str2String(
											str.substring(0, 1 * 2), 1);
									str = str.substring(1 * 2);
									contents0x = str.substring(0, Integer.parseInt(infoLength) * 2);
									msgs.setTerID(terID);
									msgs.setDirection(directions);
									msgs.setInfoLength(infoLength);
									msgs.setContents0x(contents0x);
									msgs.setProNum(proNum);
									if (directions
											.equals(ConfigManager.putBack)) {
										msgs.setContents(contents0x);
									} else if (directions
											.equals(ConfigManager.readBack)) {
										StringBuffer sb=new StringBuffer("");
										for(int i=0;i<Integer.parseInt(infoLength)/12;i++){
											String info=contents0x.substring(0, 12*2);
											if(info.substring(0, 2).equals(ConfigManager.success)){
												sb.append("有效");
											}else{
												sb.append("无效");
											}
											info=info.substring(1*2);
											sb.append(ProtocolUtil.strASICC2String(info, 11)+";");
											contents0x = contents0x.substring(12 * 2);
										}
										msgs.setContents(sb.toString());
									}
									this.executeThread.addMsg(msgs);
									break;
								case 10:// 设置海拔高度 0a
									msgs = new Event();
									terID = str.substring(0, 6 * 2);
									str = str.substring(6 * 2);
									directions = str.substring(0, 1 * 2);
									str = str.substring(1 * 2);
									infoLength = ProtocolUtil.str2String(
											str.substring(0, 1 * 2), 1);
									str = str.substring(1 * 2);
									contents0x = str.substring(0, Integer.parseInt(infoLength) * 2);
									msgs.setTerID(terID);
									msgs.setDirection(directions);
									msgs.setInfoLength(infoLength);
									msgs.setContents0x(contents0x);
									msgs.setProNum(proNum);
									if (directions
											.equals(ConfigManager.putBack)) {
										msgs.setContents(contents0x);
									} else if (directions
											.equals(ConfigManager.readBack)) {
										msgs.setContents(Integer.parseInt(contents0x,16)+"");
									}
									this.executeThread.addMsg(msgs);
									break;
								case 11:// 设置GPS记录时间间隔 0b
									msgs = new Event();
									terID = str.substring(0, 6 * 2);
									str = str.substring(6 * 2);
									directions = str.substring(0, 1 * 2);
									str = str.substring(1 * 2);
									infoLength = ProtocolUtil.str2String(
											str.substring(0, 1 * 2), 1);
									str = str.substring(1 * 2);
									contents0x = str.substring(0, Integer.parseInt(infoLength) * 2);
									msgs.setTerID(terID);
									msgs.setDirection(directions);
									msgs.setInfoLength(infoLength);
									msgs.setContents0x(contents0x);
									msgs.setProNum(proNum);
									if (directions
											.equals(ConfigManager.putBack)) {
										msgs.setContents(contents0x);
									} else if (directions
											.equals(ConfigManager.readBack)) {
										msgs.setContents(Integer.parseInt(contents0x,16)+"");
									}
									this.executeThread.addMsg(msgs);
									break;
								case 12:// 设置固定导航点 0c ???????????????
									msgs = new Event();
									terID = str.substring(0, 6 * 2);
									str = str.substring(6 * 2);
									directions = str.substring(0, 1 * 2);
									str = str.substring(1 * 2);
									infoLength = ProtocolUtil.str2String(
											str.substring(0, 1 * 2), 1);
									str = str.substring(1 * 2);
									contents0x = str.substring(0, Integer.parseInt(infoLength)* 2);
									msgs.setTerID(terID);
									msgs.setDirection(directions);
									msgs.setInfoLength(infoLength);
									msgs.setContents0x(contents0x);
									msgs.setProNum(proNum);
									if (directions
											.equals(ConfigManager.putBack)) {
										msgs.setContents(contents0x);
									} else if (directions
											.equals(ConfigManager.readBack)) {
										StringBuffer sb=new StringBuffer("");
										for(int i=0;i<Integer.parseInt(infoLength)/29;i++){
											String info=contents0x.substring(0, 29*2);
											sb.append(info.substring(0, 14));
											info=info.substring(7*2);
											sb.append(info.substring(0, 2));
											info=info.substring(1*2);
											sb.append(info.substring(0, 20));
											info=info.substring(10*2);
											sb.append(info.substring(0, 2));
											info=info.substring(1*2);
											sb.append(info.substring(0, 18));
											info=info.substring(9*2);
											sb.append(info.substring(0, 2));
											info=info.substring(1*2);
											sb.append(";");
											contents0x = contents0x.substring(29 * 2);
										}
										msgs.setContents(sb.toString());
									}
									this.executeThread.addMsg(msgs);
									break;
								case 13:// 设置导航线路 0d
									msgs = new Event();
									terID = str.substring(0, 6 * 2);
									str = str.substring(6 * 2);
									directions = str.substring(0, 1 * 2);
									str = str.substring(1 * 2);
									infoLength = ProtocolUtil.str2String(
											str.substring(0, 1 * 2), 1);
									str = str.substring(1 * 2);
									contents0x = str.substring(0,
											Integer.parseInt(infoLength) * 2);
									msgs.setTerID(terID);
									msgs.setDirection(directions);
									msgs.setInfoLength(infoLength);
									msgs.setContents0x(contents0x);
									msgs.setProNum(proNum);
									if (directions
											.equals(ConfigManager.putBack)) {
										msgs.setContents(contents0x);
									} else if (directions
											.equals(ConfigManager.readBack)) {
										StringBuffer sb=new StringBuffer("");
										sb.append(ConfigManager.linenum[Integer.parseInt(contents0x.substring(0, 2),16)]);
										if(contents0x.substring(2, 4).equals(ConfigManager.success)){
											sb.append("有效");
										}else{
											sb.append("无效");
										}
										contents0x.substring(2*2);
										for(int i=0;i<Integer.parseInt(infoLength)/29;i++){
											String info=contents0x.substring(0, 29*2);
											sb.append(info.substring(0, 14));
											info=info.substring(7*2);
											sb.append(info.substring(0, 2));
											info=info.substring(1*2);
											sb.append(info.substring(0, 20));
											info=info.substring(10*2);
											sb.append(info.substring(0, 2));
											info=info.substring(1*2);
											sb.append(info.substring(0, 18));
											info=info.substring(9*2);
											sb.append(info.substring(0, 2));
											info=info.substring(1*2);
											sb.append(";");
											contents0x = contents0x.substring(29 * 2);
										}
										msgs.setContents(sb.toString());
									}
									this.executeThread.addMsg(msgs);
									break;
								case 14:// 设置短信中心号码 0e
									msgs = new Event();
									terID = str.substring(0, 6 * 2);
									str = str.substring(6 * 2);
									directions = str.substring(0, 1 * 2);
									str = str.substring(1 * 2);
									infoLength = ProtocolUtil.str2String(
											str.substring(0, 1 * 2), 1);
									str = str.substring(1 * 2);
									contents0x = str.substring(0, Integer.parseInt(infoLength) * 2);
									msgs.setTerID(terID);
									msgs.setDirection(directions);
									msgs.setInfoLength(infoLength);
									msgs.setContents0x(contents0x);
									msgs.setProNum(proNum);
									if (directions
											.equals(ConfigManager.putBack)) {
										msgs.setContents(contents0x);
									} else if (directions
											.equals(ConfigManager.readBack)) {
										msgs.setContents(ProtocolUtil.strASICC2String(contents0x, 11));
									}
									this.executeThread.addMsg(msgs);
									break;
								case 15:// 设置用户密码 0f
									msgs = new Event();
									terID = str.substring(0, 6 * 2);
									str = str.substring(6 * 2);
									directions = str.substring(0, 1 * 2);
									str = str.substring(1 * 2);
									infoLength = ProtocolUtil.str2String(
											str.substring(0, 1 * 2), 1);
									str = str.substring(1 * 2);
									contents0x = str.substring(0, Integer.parseInt(infoLength) * 2);
									msgs.setTerID(terID);
									msgs.setDirection(directions);
									msgs.setInfoLength(infoLength);
									msgs.setContents0x(contents0x);
									msgs.setProNum(proNum);
									if (directions
											.equals(ConfigManager.putBack)) {
										msgs.setContents(contents0x);
									} else if (directions
											.equals(ConfigManager.readBack)) {
										msgs.setContents(ProtocolUtil.strASICC2String(contents0x, 13));
									}
									this.executeThread.addMsg(msgs);
									break;
								case 16:// 设置Tracker下GPRS发送时间 10
									msgs = new Event();
									terID = str.substring(0, 6 * 2);
									str = str.substring(6 * 2);
									directions = str.substring(0, 1 * 2);
									str = str.substring(1 * 2);
									infoLength = ProtocolUtil.str2String(
											str.substring(0, 1 * 2), 1);
									str = str.substring(1 * 2);
									contents0x = str.substring(0, Integer.parseInt(infoLength) * 2);
									msgs.setTerID(terID);
									msgs.setDirection(directions);
									msgs.setInfoLength(infoLength);
									msgs.setContents0x(contents0x);
									msgs.setProNum(proNum);
									if (directions
											.equals(ConfigManager.putBack)) {
										msgs.setContents(contents0x);
									} else if (directions
											.equals(ConfigManager.readBack)) {
										msgs.setContents(Integer.parseInt(contents0x, 16)+"");
									}
									this.executeThread.addMsg(msgs);
									break;
								case 17:// 设置Tracker下GPS采集时间间隔 11
									msgs = new Event();
									terID = str.substring(0, 6 * 2);
									str = str.substring(6 * 2);
									directions = str.substring(0, 1 * 2);
									str = str.substring(1 * 2);
									infoLength = ProtocolUtil.str2String(
											str.substring(0, 1 * 2), 1);
									str = str.substring(1 * 2);
									contents0x = str.substring(0, Integer.parseInt(infoLength) * 2);
									msgs.setTerID(terID);
									msgs.setDirection(directions);
									msgs.setInfoLength(infoLength);
									msgs.setContents0x(contents0x);
									msgs.setProNum(proNum);
									if (directions
											.equals(ConfigManager.putBack)) {
										msgs.setContents(contents0x);
									} else if (directions
											.equals(ConfigManager.readBack)) {
										msgs.setContents(Integer.parseInt(contents0x, 16)+"");
									}
									this.executeThread.addMsg(msgs);
									break;
								case 18:// 12
									break;
								case 19:// 13
									break;
								case 20:// 14
									break;
								case 21:// 15
									break;
								case 22:// 16
									break;
								case 23:// 17
									break;
								case 24:// 设置用户身高 18
									msgs = new Event();
									terID = str.substring(0, 6 * 2);
									str = str.substring(6 * 2);
									directions = str.substring(0, 1 * 2);
									str = str.substring(1 * 2);
									infoLength = ProtocolUtil.str2String(
											str.substring(0, 1 * 2), 1);
									str = str.substring(1 * 2);
									contents0x = str.substring(0, Integer.parseInt(infoLength) * 2);
									msgs.setTerID(terID);
									msgs.setDirection(directions);
									msgs.setInfoLength(infoLength);
									msgs.setContents0x(contents0x);
									msgs.setProNum(proNum);
									if (directions
											.equals(ConfigManager.putBack)) {
										msgs.setContents(contents0x);
									} else if (directions
											.equals(ConfigManager.readBack)) {
										msgs.setContents(Integer.parseInt(contents0x, 16)+"");
									}
									this.executeThread.addMsg(msgs);
									break;
								case 25:// 设置用户体重 19
									msgs = new Event();
									terID = str.substring(0, 6 * 2);
									str = str.substring(6 * 2);
									directions = str.substring(0, 1 * 2);
									str = str.substring(1 * 2);
									infoLength = ProtocolUtil.str2String(
											str.substring(0, 1 * 2), 1);
									str = str.substring(1 * 2);
									contents0x = str.substring(0, Integer.parseInt(infoLength) * 2);
									msgs.setTerID(terID);
									msgs.setDirection(directions);
									msgs.setInfoLength(infoLength);
									msgs.setContents0x(contents0x);
									msgs.setProNum(proNum);
									if (directions
											.equals(ConfigManager.putBack)) {
										msgs.setContents(contents0x);
									} else if (directions
											.equals(ConfigManager.readBack)) {
										msgs.setContents(Integer.parseInt(contents0x, 16)+"");
									}
									this.executeThread.addMsg(msgs);
									break;
								case 26:// 设置计步器灵敏度 1a
									msgs = new Event();
									terID = str.substring(0, 6 * 2);
									str = str.substring(6 * 2);
									directions = str.substring(0, 1 * 2);
									str = str.substring(1 * 2);
									infoLength = ProtocolUtil.str2String(
											str.substring(0, 1 * 2), 1);
									str = str.substring(1 * 2);
									contents0x = str.substring(0, Integer.parseInt(infoLength) * 2);
									msgs.setTerID(terID);
									msgs.setDirection(directions);
									msgs.setInfoLength(infoLength);
									msgs.setContents0x(contents0x);
									msgs.setProNum(proNum);
									if (directions
											.equals(ConfigManager.putBack)) {
										msgs.setContents(contents0x);
									} else if (directions
											.equals(ConfigManager.readBack)) {
										msgs.setContents(Integer.parseInt(contents0x, 16)+"");
									}
									this.executeThread.addMsg(msgs);
									break;
								case 27:// GPS Track recond数据上传 1b
									msgs = new Event();
									terID = str.substring(0, 6 * 2);
									str = str.substring(6 * 2);
									directions = str.substring(0, 1 * 2);
									str = str.substring(1 * 2);
									infoLength = ProtocolUtil.str2String(
											str.substring(0, 1 * 2), 1);
									str = str.substring(1 * 2);
									contents0x = str.substring(0,
											Integer.parseInt(infoLength) * 2);
									msgs.setTerID(terID);
									msgs.setDirection(directions);
									msgs.setInfoLength(infoLength);
									msgs.setContents0x(contents0x);
									msgs.setProNum(proNum);
									StringBuffer sb=new StringBuffer("");
									for (int i = 0; i < Integer
											.parseInt(infoLength) / 15; i++) {
										msgs = new Event();
										dateTime = str.substring(0, 6 * 2);
										dateTime = ProtocolUtil
												.getSimpleFormatDateTime(dateTime);
										str = str.substring(6 * 2);
										latitude = str.substring(0, 4 * 2);
										latitude = new Double(Long.parseLong(
												latitude, 16)) / 1800000 + "";
										if((latitude.indexOf(".")+6)<latitude.length())
											latitude=latitude.substring(0, latitude.indexOf(".")+7);
										str = str.substring(4 * 2);
										longitude = str.substring(0, 4 * 2);
										longitude = new Double(Long.parseLong(
												longitude, 16)) / 1800000 + "";
										if((longitude.indexOf(".")+6)<longitude.length())
											longitude=longitude.substring(0, longitude.indexOf(".")+7);
										str = str.substring(4 * 2);
										statusBits0x = str.substring(0, 1 * 2);
										statusBits = ProtocolUtil
												.str2ByteString(statusBits0x, 8);
										if (statusBits.substring(5, 6).equals(
												"0")) {
											longitude = "-" + longitude;
										}
										if (statusBits.substring(6, 7).equals(
												"0")) {
											latitude = "-" + latitude;
										}
										sb.append(dateTime).append(latitude).append(longitude);
										str=str.substring(1*2);
									}
									msgs.setContents(sb.toString());
									this.executeThread.addMsg(msgs);
									break;
								default:
									break;
								}
								/*下发
								 * if (GetPutInfoThread.hm.containsKey(terID)) {
									putMsg = (Event) GetPutInfoThread.hm
											.get(terID);
									if (!putMsg.getProNum().equals("81")
											& !putMsg.getProNum().equals("82")
											& !putMsg.getProNum().equals("84")
											& !putMsg.getProNum().equals("85")
											& !putMsg.getProNum().equals("86")
											& !putMsg.getProNum().equals("41")
											& !putMsg.getProNum().equals("42")) {
										if (putMsg.getProNum().equals("83")) {
											result.append(
													StartServer.MSG_START_FLAG)
													.append(putMsg.getProNum())
													.append(putMsg.getTerID())
													.append(putMsg.getSerNum())
													.append(putMsg
															.getInfoLength())
													.append(putMsg
															.getTargetCode())
													.append(putMsg
															.getLatitude())
													.append(putMsg
															.getLongitude())
													.append(putMsg
															.getStatusBits())
													.append(StartServer.MSG_END_FLAG);
											GetPutInfoThread.hm.remove(terID);
										} else if (putMsg.getProNum().equals(
												"43")) {
											result.append(
													StartServer.MSG_START_FLAG)
													.append(putMsg.getProNum())
													.append("0000")
													.append(putMsg.getTerID())
													.append(putMsg.getRemark())
													.append(StartServer.MSG_END_FLAG);
											GetPutInfoThread.hm.remove(terID);
										} else {
											result.append(
													StartServer.MSG_START_FLAG)
													.append(putMsg.getProNum())
													.append(putMsg.getTerID())
													.append(putMsg
															.getDirection())
													.append(putMsg
															.getInfoLength())
													.append(putMsg
															.getContents())
													.append(StartServer.MSG_END_FLAG);
										}
									}
									GetPutInfoThread.hm.remove(terID);
								}*/
								if (result.toString().startsWith(
										StartServer.MSG_START_FLAG)
										&& result.toString().endsWith(
												StartServer.MSG_END_FLAG)) {
									logger.info("SHW#终端连接信息:" + terID);
									msg.setMsgInfo(result.toString());
									msg.setId(terID);
									msg.setUppack(this.channelPack);
									this.sendThread.addMsg(msg);
								}
								this.channelPack.updateTime = System
										.currentTimeMillis();
							}
						} else {
							logger.info("SHW#接受终端:" + this.channelPack.getId()
									+ "的非法信息=" + str);
						}
					} catch (Exception e1) {
						if (this.argument.getUppack() != null)
							this.argument.getUppack().closeChannel();
						// e1.printStackTrace();
						logger.info("SHW#终端非法信息:" + e1);
					}
					setRunning(false);
				}
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
			e.printStackTrace();

		}
	}

	public Event setEventValue(String str) {
		Event msg = new Event();
		StringBuffer result = new StringBuffer("");
		proNum = str.substring(0, 1 * 2);
		int intNum = Integer.parseInt(proNum);
		str = str.substring(1 * 2);
		switch (intNum) {
		case 81:// gps地址
			terID = str.substring(0, 6 * 2);
			str = str.substring(6 * 2);
			serNum0x = str.substring(0, 2 * 2);
			serNum = ProtocolUtil.str2String(serNum0x, 2);
			str = str.substring(2 * 2);
			infoLength = ProtocolUtil.str2String(str.substring(0, 1 * 2), 1);
			str = str.substring(1 * 2);
			for (int i = 0; i < (Integer.parseInt(infoLength) / 15); i++) {
				dateTime = str.substring(0, 6 * 2);
				dateTime = ProtocolUtil.getSimpleFormatDateTime(dateTime);
				str = str.substring(6 * 2);
				latitude = str.substring(0, 4 * 2);
				latitude = new Double(Integer.parseInt(latitude, 16)) / 1800000
						+ "";
				if((latitude.indexOf(".")+6)<latitude.length())
					latitude=latitude.substring(0, latitude.indexOf(".")+7);
				str = str.substring(4 * 2);
				longitude = str.substring(0, 4 * 2);
				longitude = new Double(Integer.parseInt(longitude, 16))
						/ 1800000 + "";
				if((longitude.indexOf(".")+6)<longitude.length())
					longitude=longitude.substring(0, longitude.indexOf(".")+7);
				str = str.substring(4 * 2);
				statusBits = str.substring(0, 1 * 2);
				statusBits = ProtocolUtil.str2ByteString(statusBits, 8);
				if (statusBits.substring(5, 6).equals("0")) {
					longitude = "-" + longitude;
				}
				if (statusBits.substring(6, 7).equals("0")) {
					latitude = "-" + latitude;
				}
				str = str.substring(1 * 2);
				msg.setTerID(terID);
				msg.setProNum(proNum);
				msg.setSerNum(serNum);
				msg.setInfoLength(infoLength);
				msg.setDateTime(dateTime);
				msg.setLatitude(latitude);
				msg.setLongitude(longitude);
				msg.setStatusBits(statusBits);
				msg.setStation(station);
				msg.setTargetCode(targetCode);
				msg.setSteps(steps);
				msg.setDistance(distance);
				msg.setCalorie(calorie);
				msg.setVoltage(voltage);
				msg.setSignal(signal);
				msg.setReservation(reservation);
				msg.setVelocity(velocity);
				msg.setCourse(course);
				msg.setTargetReservation(targetReservation);
				msg.setIsSuccess(isSuccess);
				msg.setEcgContent(ecgContent);
				msg.setHeartRate(heartRate);
			}

			result.append(proNum).append(terID).append(serNum0x)
					.append(StartServer.ISuCCESS)
					.append(StartServer.MSG_END_FLAG);
			break;
		case 82:// 基站
			terID = str.substring(0, 6 * 2);
			str = str.substring(6 * 2);
			serNum0x = str.substring(0, 2 * 2);
			serNum = ProtocolUtil.str2String(serNum0x, 2);
			str = str.substring(2 * 2);
			infoLength = ProtocolUtil.str2String(str.substring(0, 1 * 2), 1);
			str = str.substring(1 * 2);
			for (int i = 0; i < (Integer.parseInt(infoLength) / 15); i++) {
				dateTime = str.substring(0, 6 * 2);
				dateTime = ProtocolUtil.getSimpleFormatDateTime(dateTime);
				str = str.substring(6 * 2);
				station = str.substring(0, 8 * 2);
				str = str.substring(8 * 2);
				statusBits = str.substring(0, 1 * 2);
				str = str.substring(1 * 2);
			}

			result.append(proNum).append(terID).append(serNum0x)
					.append(StartServer.ISuCCESS)
					.append(StartServer.MSG_END_FLAG);
			break;
		case 83:// 目标地址 下行
			terID = str.substring(0, 6 * 2);
			str = str.substring(6 * 2);
			serNum = str.substring(0, 2 * 2);
			str = str.substring(2 * 2);
			isSuccess = str.substring(0, 1 * 2);
			proNum = "83";
			result.append(proNum).append(terID).append("0003")
					.append("02026B3F3E0C3D909B06");
			break;
		case 84:// 心率
			terID = str.substring(0, 6 * 2);
			str = str.substring(6 * 2);
			serNum0x = str.substring(0, 2 * 2);
			serNum = ProtocolUtil.str2String(serNum0x, 2);
			str = str.substring(2 * 2);
			infoLength = ProtocolUtil.str2String(str.substring(0, 1 * 2), 1);
			str = str.substring(1 * 2);

			dateTime = str.substring(0, 6 * 2);
			dateTime = ProtocolUtil.getSimpleFormatDateTime(dateTime);
			str = str.substring(6 * 2);
			for (int i = 0; i < (Integer.parseInt(infoLength) / 2); i++) {
				heartRate = str.substring(0, 1 * 2);
				str = str.substring(1 * 2);
			}
			result.append(proNum).append(terID).append(serNum0x)
					.append(StartServer.ISuCCESS)
					.append(StartServer.MSG_END_FLAG);
			break;
		case 85:// 心电图
			terID = str.substring(0, 6 * 2);
			str = str.substring(6 * 2);
			serNum0x = str.substring(0, 2 * 2);
			serNum = ProtocolUtil.str2String(serNum0x, 2);
			str = str.substring(2 * 2);
			infoLength = ProtocolUtil.str2String(str.substring(0, 1 * 2), 1);
			str = str.substring(1 * 2);

			dateTime = str.substring(0, 6 * 2);
			dateTime = ProtocolUtil.getSimpleFormatDateTime(dateTime);
			str = str.substring(6 * 2);
			for (int i = 0; i < (Integer.parseInt(infoLength) / 4); i++) {
				ecgContent = str.substring(0, 2 * 2);
				str = str.substring(2 * 2);
			}
			result.append(proNum).append(terID).append(serNum0x)
					.append(StartServer.ISuCCESS)
					.append(StartServer.MSG_END_FLAG);
			break;
		case 86:// 计步器
			terID = str.substring(0, 6 * 2);
			str = str.substring(6 * 2);
			serNum0x = str.substring(0, 2 * 2);
			serNum = ProtocolUtil.str2String(serNum0x, 2);
			str = str.substring(2 * 2);
			infoLength = ProtocolUtil.str2String(str.substring(0, 1 * 2), 1);
			str = str.substring(1 * 2);
			dateTime = str.substring(0, 6 * 2);
			dateTime = ProtocolUtil.getSimpleFormatDateTime(dateTime);
			str = str.substring(6 * 2);
			steps = ProtocolUtil.str2String(str.substring(0, 2 * 2), 2);
			str = str.substring(2 * 2);
			distance = Double.parseDouble(ProtocolUtil.str2String(
					str.substring(0, 2 * 2), 2))
					/ 1000 + "";
			str = str.substring(2 * 2);
			calorie = Double.parseDouble(ProtocolUtil.str2String(
					str.substring(0, 4 * 2), 4))
					/ 1000 + "";
			str = str.substring(4 * 2);
			result.append(proNum).append(terID).append(serNum0x)
					.append(StartServer.ISuCCESS)
					.append(StartServer.MSG_END_FLAG);
			break;
		case 41:// 心跳包
			reservation = str.substring(0, 2 * 2);
			str = str.substring(2 * 2);
			terID = str.substring(0, 6 * 2);
			str = str.substring(6 * 2);
			serNum0x = str.substring(0, 2 * 2);
			serNum = ProtocolUtil.str2String(serNum0x, 2);
			str = str.substring(2 * 2);
			infoLength = ProtocolUtil.str2String(str.substring(0, 1 * 2), 1);
			str = str.substring(1 * 2);
			dateTime = str.substring(0, 6 * 2);
			dateTime = ProtocolUtil.getSimpleFormatDateTime(dateTime);
			str = str.substring(6 * 2);
			latitude = str.substring(0, 4 * 2);
			latitude = new Double(Integer.parseInt(latitude, 16)) / 1800000
					+ "";
			if((latitude.indexOf(".")+6)<latitude.length())
				latitude=latitude.substring(0, latitude.indexOf(".")+7);
			str = str.substring(4 * 2);
			longitude = str.substring(0, 4 * 2);
			longitude = new Double(Integer.parseInt(longitude, 16)) / 1800000
					+ "";
			if((longitude.indexOf(".")+6)<longitude.length())
				longitude=longitude.substring(0, longitude.indexOf(".")+7);
			str = str.substring(4 * 2);
			velocity = ProtocolUtil.str2String(str.substring(0, 1 * 2), 1);
			str = str.substring(1 * 2);
			course = ProtocolUtil.str2String(str.substring(0, 2 * 2), 2);
			str = str.substring(2 * 2);
			targetReservation = str.substring(0, 3 * 2);
			str = str.substring(3 * 2);
			statusBits = str.substring(0, 4 * 2);
			statusBits = ProtocolUtil.str2ByteString(statusBits, 32);
			if (statusBits.substring(29, 30).equals("0")) {
				longitude = "-" + longitude;
			}
			if (statusBits.substring(30, 31).equals("0")) {
				latitude = "-" + latitude;
			}
			str = str.substring(4 * 2);
			result.append(proNum).append(reservation).append(terID)
					.append(serNum0x).append(StartServer.MSG_END_FLAG);
			break;
		case 42:// gps数据
			terID = str.substring(0, 6 * 2);
			str = str.substring(6 * 2);
			serNum0x = str.substring(0, 2 * 2);
			serNum = ProtocolUtil.str2String(serNum0x, 2);
			infoLength = "0";
			dateTime = TimeTool.getDateTimeSecond();
			str = str.substring(2 * 2);
			voltage = ProtocolUtil.str2String(str.substring(0, 1 * 2), 1);
			str = str.substring(1 * 2);
			signal = ProtocolUtil.str2String(str.substring(0, 1 * 2), 1);
			str = str.substring(1 * 2);
			result.append(proNum).append(StartServer.MSG_END_FLAG);
			break;
		case 43:// gps数据区间 下行
			reservation = ProtocolUtil.str2String(str.substring(0, 2 * 2), 2);
			str = str.substring(2 * 2);
			terID = str.substring(0, 6 * 2);
			str = str.substring(6 * 2);
			isSuccess = str.substring(0, 1 * 2);
			str = str.substring(1 * 2);
			result.append(proNum).append(reservation).append(terID)
					.append("0A").append(StartServer.MSG_END_FLAG);
			break;

		default:
			break;
		}
		return null;
	}
}